package com.example.gsq.news.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Scroller;

import com.example.gsq.news.util.UIUtil;

public class MToggleButton extends View implements View.OnClickListener {

    private Context mContext;

    private boolean isOn;

    private Scroller scroller;

    private int viewWidth;
    private int viewHeight;
    private int lineWidth;
    private int circleRadius;
    private int currentX;
    private int currentY;

    private onToggleChangedListener onToggleChangedListener;

    public MToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        scroller = new Scroller(mContext);
        setOnClickListener(this);
        init();
    }

    private void init() {
        isOn = false;
        viewWidth = UIUtil.dp2px(mContext, 50);
        viewHeight = UIUtil.dp2px(mContext, 22);
        lineWidth = UIUtil.dp2px(mContext, 2);
        circleRadius = UIUtil.dp2px(mContext, 9);
    }

    public void setStatus(boolean isOn) {
        this.isOn = isOn;
    }

    public boolean isOn() {
        return isOn;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(viewWidth, viewHeight);
        currentX = getMeasuredHeight() / 2;
        currentY = getMeasuredHeight() / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint1 = new Paint();
        paint1.setColor(Color.BLACK);
        paint1.setStrokeWidth(lineWidth);
        canvas.drawLine(viewHeight / 2, viewHeight / 2, viewWidth - viewHeight / 2, viewHeight / 2, paint1);
        Paint paint2 = new Paint();
        paint2.setStyle(Paint.Style.FILL_AND_STROKE);
        paint2.setStrokeWidth(2);
        paint2.setColor(Color.WHITE);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        paint2.setShadowLayer(1, -2, 2, Color.GRAY);
        canvas.drawCircle(currentX, currentY, circleRadius, paint2);
    }

    @Override
    public void onClick(View v) {
        if (!scroller.computeScrollOffset() && !isOn) {
            scroller.startScroll(viewHeight / 2, viewHeight / 2, viewWidth - viewHeight, 0, 1000);
            isOn = true;
            onToggleChangedListener.onToggle(true);
        } else if (!scroller.computeScrollOffset() && isOn) {
            scroller.startScroll(viewWidth - viewHeight / 2, viewHeight / 2, -(viewWidth - viewHeight), 0, 1000);
            isOn = false;
            onToggleChangedListener.onToggle(false);
        }
        invalidate();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()) {
            currentX = scroller.getCurrX();
            currentY = scroller.getCurrY();
            invalidate();
        }
    }

    public void setOnToggleChanged(onToggleChangedListener listener) {
        this.onToggleChangedListener = listener;
    }

    public interface onToggleChangedListener {
        void onToggle(boolean isOn);
    }

}
